package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.ParamUtils;
import com.company.project.entity.ScheduledTaskLogEntity;
import com.company.project.service.ScheduledTaskLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务执行日志Controller
 */
@Api(tags = "定时任务执行日志管理")
@RestController
@RequestMapping("/scheduledTaskLog")
@Slf4j
public class ScheduledTaskLogController {

    @Autowired
    private ScheduledTaskLogService scheduledTaskLogService;

    @ApiOperation(value = "分页查询定时任务执行日志")
    @PostMapping("/listByPage")
    public DataResult listByPage(@RequestBody Map<String, Object> params) {
        try {
            Integer pageNum = ParamUtils.getInteger(params, "page");
            Integer pageSize = ParamUtils.getInteger(params, "limit");
            String taskName = ParamUtils.getString(params, "taskName");
            Integer executeStatus = ParamUtils.getInteger(params, "executeStatus");

            Page<ScheduledTaskLogEntity> page = new Page<>(pageNum, pageSize);
            QueryWrapper<ScheduledTaskLogEntity> wrapper = new QueryWrapper<>();

            if (taskName != null && !taskName.trim().isEmpty()) {
                wrapper.like("task_name", taskName);
            }
            if (executeStatus != null) {
                wrapper.eq("execute_status", executeStatus);
            }
            // 只查询未删除的记录
            wrapper.eq("deleted", 0);
            // 按执行时间倒序
            wrapper.orderByDesc("execute_time");

            IPage<ScheduledTaskLogEntity> result = scheduledTaskLogService.page(page, wrapper);
            return DataResult.success(result);
        } catch (Exception e) {
            log.error("查询定时任务执行日志异常：" + e.getMessage());
            return DataResult.fail("查询异常：" + e.getMessage());
        }
    }

    @ApiOperation(value = "根据任务ID查询执行日志")
    @GetMapping("/listByTaskId/{taskId}")
    public DataResult listByTaskId(@PathVariable Integer taskId) {
        try {
            QueryWrapper<ScheduledTaskLogEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("task_id", taskId);
            wrapper.eq("deleted", 0);
            wrapper.orderByDesc("execute_time");
            wrapper.last("LIMIT 100"); // 限制返回最近100条记录

            return DataResult.success(scheduledTaskLogService.list(wrapper));
        } catch (Exception e) {
            log.error("根据任务ID查询执行日志异常：" + e.getMessage());
            return DataResult.fail("查询异常：" + e.getMessage());
        }
    }

    @ApiOperation(value = "清空执行日志")
    @DeleteMapping("/clearLogs")
    public DataResult clearLogs() {
        try {
            // 这里可以选择物理删除或逻辑删除
            // 为了安全起见，这里使用逻辑删除
            QueryWrapper<ScheduledTaskLogEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);
            
            // 批量逻辑删除
            scheduledTaskLogService.remove(wrapper);
            return DataResult.success("日志清空成功");
        } catch (Exception e) {
            log.error("清空执行日志异常：" + e.getMessage());
            return DataResult.fail("清空失败：" + e.getMessage());
        }
    }
}
